package org.academic.texteditor.gui.plugin.text;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.KeyStroke;

import org.academic.texteditor.gui.component.PageContainer;
import org.academic.texteditor.gui.component.PersonalizedTextPane;
import org.academic.texteditor.gui.component.menu.MenuFactory;
import org.academic.texteditor.gui.feature.Page;
import org.academic.texteditor.gui.helper.ParagraphElement;
import org.academic.texteditor.gui.plugin.BasePlugin;

public class CloneTextUpPlugin extends BasePlugin {

	private static final long serialVersionUID = -200504698290349734L;
	
	private PageContainer pageContainer;

	public CloneTextUpPlugin() {
		super("cloneTextUp");
	}

	@Override
	public void initialize(PageContainer pageContainer) {
		this.pageContainer = pageContainer;
	}

	@Override
	public void activate() {
		JMenu menuEdit = MenuFactory.getEdit();
		MenuFactory.createMenuItem(menuEdit, 0, "cloneTextUp", "menu.edit.cloneTextUp", this).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_UP, ActionEvent.CTRL_MASK + ActionEvent.ALT_MASK));
	}

	@Override
	public void execute() {
		Page page = pageContainer.getActualPage();
		PersonalizedTextPane textPane = page.getText().getTextPane();
		
		ParagraphElement element = textPane.getParagraphElement();
		int offset = element.getStartOffset();
		
		textPane.insert(offset, element.getText() + "\n");
	}

	@Override
	public void deactivate() {
		JMenu menuEdit = MenuFactory.getEdit();
		MenuFactory.removeMenuItem(menuEdit, "cloneTextUp");
	}

	@Override
	public void destroy() {

	}

}
